﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Xml.Serialization;

namespace Kangaroo.Model
{
    [DebuggerDisplay("DevId={DevId}, DevChn={DevChn}, EquipId={EquipId}, EquipPhase={EquipPhase}")]
    public class ChannelInfo : IEquatable<ChannelInfo>
    {
        [XmlIgnore]
        [Browsable(false)]
        public int Key { get; set; }

        [XmlAttribute("检测装置ID")]
        public string DevId { get; set; }

        [XmlAttribute("检测通道")]
        public Int16 DevChn { get; set; }

        [XmlAttribute("电力设备ID")]
        public string EquipId { get; set; }

        [XmlAttribute("设备相别")]
        public byte EquipPhaseValue
        {
            get { return EquipPhase; }
            set { EquipPhase = value; }
        }

        [XmlIgnore]
        public EquipPhase EquipPhase { get; set; }

        public ChannelInfo()
        {
            DevId = EquipId = string.Empty;
        }

        public ChannelInfo(string devId, short devChn, string equipId, EquipPhase equipPhase)
        {
            DevId = devId;
            DevChn = devChn;
            EquipId = equipId;
            EquipPhase = equipPhase;
        }

        public ChannelInfo(int key, string devId, short devChn, string equipId, EquipPhase equipPhase)
        {
            Key = key;
            DevId = devId;
            DevChn = devChn;
            EquipId = equipId;
            EquipPhase = equipPhase;
        }

        public override string ToString()
        {
            return string.Format("检测装置ID {0}, 检测通道 {1}, 电力设备ID {2}, 设备相别 {3}", 
                DevId, DevChn, EquipId, EquipPhase);
        }

        public bool Equals(ChannelInfo other)
        {
            if(ReferenceEquals(null, other))
                return false;
            if(ReferenceEquals(this, other))
                return true;
            return Equals(other.DevId, DevId) && other.DevChn == DevChn && 
                Equals(other.EquipId, EquipId) && Equals(other.EquipPhase, EquipPhase);
        }

        public override bool Equals(object obj)
        {
            if(ReferenceEquals(null, obj))
                return false;
            if(ReferenceEquals(this, obj))
                return true;
            if(obj.GetType() != typeof(ChannelInfo))
                return false;
            return Equals((ChannelInfo)obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                int result = (DevId != null ? DevId.GetHashCode() : 0);
                result = (result * 397) ^ DevChn.GetHashCode();
                result = (result * 397) ^ (EquipId != null ? EquipId.GetHashCode() : 0);
                result = (result * 397) ^ (EquipPhase != null ? EquipPhase.GetHashCode() : 0);
                return result;
            }
        }

        public static bool operator ==(ChannelInfo left, ChannelInfo right)
        {
            return Equals(left, right);
        }

        public static bool operator !=(ChannelInfo left, ChannelInfo right)
        {
            return !Equals(left, right);
        }
    }
}
